<?php

namespace app\admin\controller;

use think\Db;

class UserMoneyLog extends Base
{
    /**
     * 获取资金列表
     */
    public function getMoneyList()
    {
        $where = [];
        input('startTime') ? $where['um.createTime'] = ['between time', [input('startTime'), input('endTime')]] : true;
        input('moneyType') ? $where['um.moneyType'] = input('moneyType') : true;
        input('orderSrc') ? $where['um.orderSrc'] = input('orderSrc') : true;
        input('targetMobile') ? $where['u.mobile'] = ['like', '%' . input('targetMobile') . '%'] : true;
        input('targetName') ? $where['u.name'] = ['like', '%' . input('targetName') . '%'] : true;

        $res = model('user_money_log')
            ->alias('um')
            ->field('um.*,u.mobile as targetMobile,u.name as targetName')
            ->join('user u', 'u.id=um.targetId', 'left')
            ->where($where)
            ->order('um.id desc')
            ->paginate(10)
            ->toArray();

        $res ? $this->suc($res) : $this->err('获取资金列表失败！');
    }

    /**
     * 获取资金详情
     */
    public function getMoneyDetail()
    {
        $where = [];
        input('id') ? $where['um.id'] = input('id') : $this->err('缺少钱包日志主键！');
        $res = model('user_money_log')
            ->alias('um')
            ->field('um.*,u.mobile as targetMobile,u.name as targetName')
            ->join('user u', 'u.id=um.targetId', 'left')
            ->where($where)
            ->find();

        $res ? $this->suc($res) : $this->err('获取资金详情失败');
    }

    /**
     * 充值
     */
    public function rechargeMoney()
    {
        $data = [];
        $e_data = [];
        $where = [];

        input('cid') ? $where['id'] = input('cid') : $this->err('缺少用户主键！');
        input('rechargeMoney/d') > 0 ? $e_data['rechargeMoney'] = input('rechargeMoney/d') : $this->err('缺少充值金额！');

        $rep = model('user')->field('money')->where($where)->find();    //查询用户钱包余额

        $rep['money'] = $rep['money'] + $e_data['rechargeMoney'];
        $data['money'] = $rep['money'];

        $d_data = [
            'leftMoney' => $rep['money'],
            'money' => $e_data['rechargeMoney'],
            'moneySn' => createOrderCode('ZJ'),
            'createTime' => createTime(),
            'moneyType' => 1,
            'targetId' => $where['id'],
            'orderSrc' => 5
        ];
        //启动事务
        Db::startTrans();
        try {
            model('user_money_log')->editData($d_data);
            $e_rep = model('user')->editData($data, $where['id']);
            //提交事务
            Db::commit();
            $e_rep ? $this->suc('充值成功！') : $this->err('充值失败！');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->err('操作失败！');
        }

    }


    /**
     *  审核提现
     * status 1-完成 2-处理中 3-已取消
     */
    public function auditMoneyTake()
    {
        $where = [];
        $data = [];
        $u_data = [];
        $l_data = [];
        $uc_data = [];
        $lg_data = [];
        $res = 0;

        input('status') ? $data['status'] = input('status') : $this->err('缺少审核状态！');
        input('id') ? $l_data['id'] = input('id') : $this->err('缺少日志主键！');
        input('cashId') ? $uc_data['id'] = input('cashId') : $this->err('缺少提现主键！');

        $u_log = model('user_money_log')->field('id, money, leftMoney')->where(['id' => $l_data['id']])->find();
        $u_log ? true : $this->err('资金日志不存在！');

        // status 1-完成 完成提现操作，改变状态
        if ($data['status'] == 1) {
            $uc_data['status'] = 1;
            $uc_res = model('user_cash')->editData($uc_data, $uc_data['id']);
            $uc_res ? $this->suc('审核通过') : $this->err('审核失败');

        }
        // status 1-已取消 取消提现操作，改变状态，将钱返还用户钱包
        if ($data['status'] == 3) {
            input('targetId') ? $u_data['id'] = input('targetId') : $this->err('缺少用户主键');
            input('money') ? $u_data['money'] = input('money') : $this->err('缺少退款金额！');
            $user_res = model('user')->field('id,money')->where($where)->find();  //查询用户钱包余额
            $user_res['money'] = $user_res['money'] - $u_data['money'];
            $lg_data = [
                'leftMoney' => $user_res['money'],
                'money' => $u_log['money'],
                'moneySn' => createOrderCode('ZJ'),
                'createTime' => createTime(),
                'moneyType' => 1,
                'targetId' => $user_res['id'],
                'orderSrc' => 6,
                'status' => 3
            ];
            $uc_data['status'] = 3;
            $u_data = [
                'id' => $user_res['id'],
                'money' => $user_res['money']
            ];
            //启动事务
            Db::startTrans();
            try {
                $u_res = model('user')->editData($u_data, $u_data['id']);    //更新用户钱包金额
                $l_res = model('user_money_log')->editData($lg_data);        //新增钱包日志
                $uc_res = model('user_cash')->editData($uc_data, $uc_data['id']); //更新提现状态
                Db::commit();
                $u_res ? $this->suc('取消成功！') : $this->err('取消失败！');
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->err('操作失败！');
            }


        }

    }

}

